<html>
<head><meta charset="utf-8"><title>Code tips and review · zulip · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/index.html">zulip</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html">Code tips and review</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="190158462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190158462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190158462">(Mar 10 2020 at 10:24)</a>:</h4>
<p>Hi Rustaceans! After lurking years, I finally got my hands on three small projects, in Rust:</p>
<ol>
<li><a href="https://gist.github.com/gsurrel/67c7a24b0b86b45a951b52e772ac0f8c" target="_blank" title="https://gist.github.com/gsurrel/67c7a24b0b86b45a951b52e772ac0f8c">Generate objects according to a probability map for a 20 y.o. game</a></li>
<li><a href="https://gist.github.com/gsurrel/d3b3903260c619814969b9799642a129" target="_blank" title="https://gist.github.com/gsurrel/d3b3903260c619814969b9799642a129">Draw an SVG image to replicate a measurement sheet for barometric readings</a></li>
<li><a href="https://gist.github.com/gsurrel/77fbd67d9978e3085bd3b6c2d6b9435b" target="_blank" title="https://gist.github.com/gsurrel/77fbd67d9978e3085bd3b6c2d6b9435b">The biggest one, where I listen <code>dmesg</code> for a newly connected Android device, wait until it's detected by <code>adb</code>, and updates the status when disconnected</a>. It's not finished, but the structure is here. </li>
</ol>
<p>I was wondering if you would have advice on making the code more idiomatic, even though both are very small. No need for a full review, though <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="190158874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190158874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190158874">(Mar 10 2020 at 10:31)</a>:</h4>
<p>For 2: you could use <code>&amp;["..."] as &amp;[&amp;str]</code> at <a href="https://gist.github.com/gsurrel/d3b3903260c619814969b9799642a129#file-barometer-rs-L27" target="_blank" title="https://gist.github.com/gsurrel/d3b3903260c619814969b9799642a129#file-barometer-rs-L27">https://gist.github.com/gsurrel/d3b3903260c619814969b9799642a129#file-barometer-rs-L27</a> to avoid an allocation</p>



<a name="190158999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190158999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190158999">(Mar 10 2020 at 10:32)</a>:</h4>
<p>For 3: enum variants are normally named CamelCase, not UPPER_SNAKE_CASE.</p>



<a name="190159112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190159112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190159112">(Mar 10 2020 at 10:34)</a>:</h4>
<p>For 3: you can read a file using <code>std::fs::read_to_string</code></p>



<a name="190159381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190159381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190159381">(Mar 10 2020 at 10:39)</a>:</h4>
<p>Other than that it looks pretty good! You may want to use rustfmt (cargo fmt) and clippy (cargo clippy) though. Rustfmt is a code formatter, while clippy is a code linter.</p>



<a name="190160583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190160583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190160583">(Mar 10 2020 at 10:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122653-zulip/topic/Code.20tips.20and.20review/near/190158999" title="#narrow/stream/122653-zulip/topic/Code.20tips.20and.20review/near/190158999">said</a>:</p>
<blockquote>
<p>For 3: enum variants are normally named CamelCase, not UPPER_SNAKE_CASE.</p>
</blockquote>
<p>Oh, I thought the compiler would yell at me for that in the same way it yells for CamelCase crate or variables names :)<br>
Thanks!</p>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122653-zulip/topic/Code.20tips.20and.20review/near/190159112" title="#narrow/stream/122653-zulip/topic/Code.20tips.20and.20review/near/190159112">said</a>:</p>
<blockquote>
<p>For 3: you can read a file using <code>std::fs::read_to_string</code></p>
</blockquote>
<p>I'll investigate that right now. I built it using the <a href="https://rust-lang-nursery.github.io/rust-cookbook/os/external.html#continuously-process-child-process-outputs" target="_blank" title="https://rust-lang-nursery.github.io/rust-cookbook/os/external.html#continuously-process-child-process-outputs">Rust Cookbook</a>, maybe that's why I built it that way. However, it doesn't apply to my reading of the uptime, where I followed <a href="https://rust-lang-nursery.github.io/rust-cookbook/file/read-write.html#read-lines-of-strings-from-a-file" target="_blank" title="https://rust-lang-nursery.github.io/rust-cookbook/file/read-write.html#read-lines-of-strings-from-a-file">this other reciepe</a>.</p>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122653-zulip/topic/Code.20tips.20and.20review/near/190159381" title="#narrow/stream/122653-zulip/topic/Code.20tips.20and.20review/near/190159381">said</a>:</p>
<blockquote>
<p>Other than that it looks pretty good! You may want to use rustfmt (cargo fmt) and clippy (cargo clippy) though. Rustfmt is a code formatter, while clippy is a code linter.</p>
</blockquote>
<p>I know their existence, I've to check how to install them and integrate them into IntelliJ IDEA (I chose this IDE as I'm very used to Android dev., and I like not to learn again shortcuts!)</p>



<a name="190160589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122653-zulip/topic/Code%20tips%20and%20review/near/190160589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122653-zulip/topic/Code.20tips.20and.20review.html#190160589">(Mar 10 2020 at 10:59)</a>:</h4>
<p>Thanks for the tips!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>